package com.rustic.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.utils.PageUtil;
import cn.xy.commons.enums.DisableStatus;
import com.rustic.server.query.FoodAdminListQuery;
import com.rustic.server.dto.FoodAddDTO;
import com.rustic.server.dto.FoodUpdateDTO;
import com.rustic.server.po.Food;
import com.rustic.server.service.FoodService;
import com.rustic.server.vo.list.FoodAdminListVO;
import com.rustic.server.vo.FoodVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[food]对应的后台管理类
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/admapi/food")
@Api(tags = {"美食推荐管理接口"})
public class FoodAdminController {

    @Autowired
    private FoodService foodService;

    @ApiOperation(value = "美食推荐-列表", notes = "权限key: food-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('food-list')")
    public Result<Page<FoodAdminListVO>> list(@Validated FoodAdminListQuery query) {
        LambdaQueryWrapper<Food> queryWrapper = foodService.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return Result.data(new Page<>());
        }
        Page<Food> pageData = foodService.page(query.toPage(), queryWrapper);
        List<Food> foodList = pageData.getRecords();
        if (CollUtil.isEmpty(foodList)) {
            return Result.data(new Page<>());
        }
        List<FoodAdminListVO> results = foodService.toAdminListVo(foodList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "美食推荐-详情", notes = "权限key: food-detail")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "美食推荐id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('food-detail')")
    public Result<FoodVO> detail(int id) {
        if (id <= 0) {
            return Result.error(FoodService.FOOD_NOT_EXIST);
        }
        return Result.data(foodService.getDetailById(id));
    }

    @ApiOperation(value = "美食推荐-增加", notes = "权限key: food-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('food-add')")
    @OperateLog(description = "增加美食推荐")
    public Result<String> add(@RequestBody @Validated FoodAddDTO foodAddDTO) {
        foodService.add(foodAddDTO);
        return Result.ok("添加美食推荐成功");
    }

    @ApiOperation(value = "美食推荐-更新", notes = "权限key: food-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('food-update')")
    @OperateLog(description = "更新美食推荐")
    public Result<String> update(@RequestBody @Validated FoodUpdateDTO foodUpdateDTO) {
        foodService.updateFood(foodUpdateDTO);
        return Result.ok("编辑美食推荐成功");
    }

    @ApiOperation(value = "美食推荐-删除", notes = "权限key: food-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('food-delete')")
    @OperateLog(description = "删除美食推荐")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(FoodService.FOOD_NOT_EXIST);
        }
        foodService.delete(id);
        return Result.ok("删除美食推荐成功");
    }

    @ApiOperation(value = "美食-发布下架", notes = "权限key: food-enabled")
    @PostMapping("/enabled")
    @PreAuthorize("@validate.hasPermission('food-enabled')")
    @OperateLog(description = "发布/下架美食")
    public Result<String> enabled(int id) {
        if (id <= 0) {
            return Result.error(FoodService.FOOD_NOT_EXIST);
        }
        int status = foodService.enabled(id);
        String action = "下架";
        if (DisableStatus.isNormal(status)) {
            action = "发布";
        }
        return Result.ok(action + "成功");
    }
}
